import styles from './index.less';
import moment from 'moment';

/* 排除 新能源预测-XZ 数据 后期可能会用，可以删掉 */
const ENERGY = '新能源预测-XZ';

/* 供需分析toolTip展示 */
export const superToolShow = (data) => {
  return (
    <div className={styles.superContainer}>
      <span className={styles.date}>{data?.[0]?.axisValueLabel}</span>
      {data.map((item, index) => {
        const value =
          item?.value === '-'
            ? '-'
            : item?.seriesName === '调整量'
            ? Number(item?.value)?.toFixed(3) > 0
              ? `+${Number(item?.value)?.toFixed(3)}`
              : Number(item?.value)?.toFixed(3)
            : Number(item?.value)?.toFixed(3);
        return (
          <span key={index}>
            <font className={styles.seriesName}>{item?.seriesName || ''}:</font>
            <font className={styles.superVal} style={{ color: item?.color || '' }}>
              {value}
            </font>
          </span>
        );
      })}
    </div>
  );
};

/* 合并偏差值 */
export const mergeSupplyData = (obj, data) => {
  let supplyData = obj?.filter(
    (item) =>
      item.type === 'line' && item.name !== '新能源预测-DR' && item.name !== '新能源预测-HN',
  );
  const subData = supplyData.filter(
    (item) => item.name != '系统负荷-联络线' && item.name !== '新能源预测-XZ',
  );
  const DWData = obj?.filter((item) => item.name === '新能源预测-DW')?.[0];
  const DWFlag = DWData && DWData?.list?.some((item) => item?.value !== '-');
  let biasData = DWFlag ? data?.list : data?.adjustments;

  biasData = biasData.map((item, index) => {
    let sum = 0;
    let xAxis = '';
    subData.map((val) => {
      let list = val?.list || [];
      xAxis = list?.[index]?.xAxis;
      if (DWFlag) {
        sum += list?.[index]?.value === '-' ? 0 : list?.[index]?.value * 1;
      } else sum = item || 0;
    });
    return {
      xAxis,
      value: DWFlag ? sum + item : sum,
    };
  });

  const _obj = {
    ...data,
    list: biasData,
  };
  supplyData.push(_obj);
  return supplyData;
};

/*  格式化00:00变为24:00 */
const findCharts = (val, index) => {
  if (moment(Number(val)).format('HHmm') === '0000') {
    return moment(Number(val)).add(-1, 'days').format('YYYYMMDD') + ' 2400';
  } else {
    return moment(Number(val)).format('YYYYMMDD HHmm');
  }
};

/* 初始化供需分析配置 */
export const fetchSupplyToolData = (datas) => {
  const data = datas.filter((i) => i.name !== ENERGY);
  const nameArr = ['系统负荷-联络线', '调整后-出力预测'];
  let xAxisData = [];
  let _data = data.map((item, index) => {
    _data = item?.list?.map((item, index) => {
      if (xAxisData.length < 96) {
        xAxisData.push(findCharts(item.xAxis));
      }
      return {
        ...item,
        xAxis: findCharts(item.xAxis, index),
      };
    });
    return {
      ...item,
      data: _data,
      areaStyle: {
        color: nameArr.includes(item?.name) ? '' : item?.color || '',
        opacity: nameArr.includes(item?.name) ? 0 : 0.2,
      },
      stack: nameArr.includes(item?.name) ? index : 1,
      symbolSize: 0,
      symbol: 'circle',
      smooth: true,
      itemStyle: {
        normal: {
          lineStyle: {
            width: 1, //设置线条粗细
          },
        },
      },
    };
  });
  _data = _data.filter((item) => item.type === 'line');
  const result = fetchSuperTool(_data);
  return result;
};

/**
 * 初始化superTool
 * @param date
 */
export const fetchSuperTool = (data) => {
  const xtfhObj =
    data.find((item) => {
      return item.name === '系统负荷-联络线';
    }) ?? {};
  const tzclObj =
    data.find((item) => {
      return item.name === '调整后-出力预测';
    }) ?? {};
  const hdObj =
    data.find((item) => {
      return item.name === '火电最小';
    }) ?? {};
  const sdObj =
    data.find((item) => {
      return item.name === '水电计划';
    }) ?? {};
  const xnydObj =
    data.find((item) => {
      return item.name === '新能源预测-DW';
    }) ?? {};
  const result = [
    {
      seriesName: '系统负荷-联络线',
      value: xtfhObj?.data[0]?.value || '-',
      axisValueLabel: xtfhObj?.data[0]?.xAxis || '',
      seriesIndex: 0,
      seriesType: 'line',
      color: '#61F4F4',
    },
    {
      seriesName: '火电最小',
      value: hdObj?.data[0]?.value || '-',
      axisValueLabel: hdObj?.data[0]?.xAxis || '',
      color: '#048DE6',
      seriesIndex: 1,
      seriesType: 'line',
    },
    {
      seriesName: '水电计划',
      value: sdObj?.data[0]?.value || '-',
      axisValueLabel: sdObj?.data[0]?.xAxis || '',
      color: '#61B1F4',
      seriesIndex: 2,
      seriesType: 'line',
    },
    {
      seriesName: '新能源预测-DW',
      value: xnydObj?.data[0]?.value || '-',
      axisValueLabel: xnydObj?.data[0]?.xAxis || '',
      seriesIndex: 3,
      color: '#28B28B',
      seriesType: 'line',
    },
    {
      seriesName: '调整后-出力预测',
      value: tzclObj?.data[0]?.value || '-',
      axisValueLabel: tzclObj?.data[0]?.xAxis || '',
      color: '#EC903B',
      seriesIndex: 4,
      seriesType: 'line',
    },
  ];
  return result;
};

export const add = (num1, num2) => {
  let r1, r2, m;
  try {
    r1 = num1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = num2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  return (num1 * m + num2 * m) / m;
};

export const subtract = (num1, num2) => {
  let r1, r2, m, n;
  try {
    r1 = num1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = num2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  n = r1 >= r2 ? r1 : r2;
  return ((num1 * m - num2 * m) / m).toFixed(n);
};
